package com.fintech.third.service.mobile;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.UUID;

import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;

import org.apache.log4j.Logger;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.fintech.common.util.ShaUtil;
import com.fintech.third.utils.HttpUtil;
import com.fintech.third.utils.MD5Util;
import com.fintech.third.utils.ThirdPropertiesInitUtil;

/**
 * 
* @ClassName: PhoneVertifyServiceImp 
* @Description:手机号码验证服务类  
* @author 李瑶
* @date 2018年7月24日 下午5:42:44 
*
 */
@Service("phoneVertifyService")
public class PhoneVertifyServiceImp implements PhoneVertifyService {

	/**
	 * 
	* @Title: checkLocalPhone 
	* @Description: 检测是否是本机号码 
	* @param token
	* @param phone
	* @param requesterType 请求类型0:APP；1:WAP
	* @return       
	* @author 李瑶
	* @date 2018年7月24日 下午5:40:21
	 */
	/*@Override
	public String checkLocalPhone(String token,String phoneNum,String requesterType) {
		try {
			Properties properties=ThirdPropertiesInitUtil.initProperties("/configuration/external.properties");
			
			String version = "1.0";
			String openType = "1";
			String appId = properties.getProperty("mobile.vertify.appId");
			String appkey = properties.getProperty("mobile.vertify.appKey");
			String msgId = UUID.randomUUID().toString().replace("-", "");
			String timestamp = getTimeStamp();
			
			String phoneNumMg = phoneNum + appkey + timestamp;
			String phoneNumEncrypt = ShaUtil.Encrypt(phoneNumMg, "SHA-256");
			phoneNumEncrypt = phoneNumEncrypt.toUpperCase();
			String signMg = appId + msgId + phoneNumEncrypt + timestamp + token + version;
			String sign = sha256_HMAC(signMg, appkey);

			JSONObject body = new JSONObject();
			JSONObject header = new JSONObject();
			header.put("version",version);
			header.put("msgId",msgId);
			header.put("timestamp",timestamp);
			header.put("appId",appId);
			body.put("requesterType", requesterType);
			body.put("openType", openType);
			body.put("phoneNum", phoneNumEncrypt);
			body.put("token", token);
			body.put("sign", sign);
			JSONObject sendParam = new JSONObject();
			sendParam.put("header", header);
			sendParam.put("body", body);
			return  HttpUtil.okHttpPost("https://www.cmpassport.com/openapi/rs/tokenValidate", sendParam.toJSONString());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error(e+"", e);
		}

		return "";
		
	
	}*/
	
	
	/**
	 * 
	* @Title: getUserInfo 
	* @Description:获取运营商本机用户信息  
	* @param token
	* @return       
	* @author 李瑶
	* @date 2018年8月7日 下午5:37:41
	 */
	/*public String getUserInfo(String token){
		try {
			Properties properties=ThirdPropertiesInitUtil.initProperties("/configuration/external.properties");
			
			String version = "2.0";
			String strictcheck = "0";
			String appId = properties.getProperty("mobile.vertify.appId");
			String appKey = properties.getProperty("mobile.vertify.appKey");
			String msgId = UUID.randomUUID().toString().replace("-", "");
			String systemtime = getTimeStamp();
			String sign = MD5Util.getMD5(appId + version + msgId + systemtime +
					strictcheck + token + appKey);
			JSONObject sendParam = new JSONObject();
			sendParam.put("appid", appId);
			sendParam.put("msgid", msgId);
			sendParam.put("sign", sign);
			sendParam.put("strictcheck", strictcheck);
			sendParam.put("systemtime", systemtime);
			sendParam.put("token", token);
			sendParam.put("version", version);
			return  HttpUtil.okHttpPost("https://www.cmpassport.com/unisdk/rsapi/loginTokenValidate", sendParam.toJSONString());
		} catch (Exception e) {
			MarvelsLogUtil.getInstance().error("查询运营商用户数据",e);
		}
		return "";
		
	}*/
	
	@Override
	public Map<String, String> getParam() {
		Properties properties=ThirdPropertiesInitUtil.initProperties("/configuration/external.properties");
		Map<String, String> returnMap = new HashMap<>();
		String appId = properties.getProperty("mobile.vertify.appId");
		String appKey = properties.getProperty("mobile.vertify.appKey");
		returnMap.put("appId", appId);
		return returnMap;
	}

	/**
	 * 
	* @Title: getTimeStamp 
	* @Description: 获取时间戳，精确到毫秒数 
	* @return       
	* @author 李瑶
	* @date 2018年8月8日 上午9:53:48
	 */
	private String getTimeStamp(){
		Date date = new Date();
		SimpleDateFormat sb = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		return sb.format(date);
	}
	
	
	public static String sha256_HMAC(String message, String secret) {
	    String hash = "";
	    try {
	        Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
	        SecretKeySpec secret_key = new SecretKeySpec(secret.getBytes(), "HmacSHA256");
	        sha256_HMAC.init(secret_key);
	        byte[] bytes = sha256_HMAC.doFinal(message.getBytes());
	        hash = byte2hex(bytes);
	        System.out.println(hash);
	    } catch (Exception e) {
	      
	    }
	    return hash;
	}

	private static String byte2hex(byte[] b) {
	    StringBuilder hs = new StringBuilder();
	    String stmp;
	    for (int n = 0; b != null && n < b.length; n++) {
	        stmp = Integer.toHexString(b[n] & 0XFF);
	        if (stmp.length() == 1)
	            hs.append('0');
	        hs.append(stmp);
	    }
	    return hs.toString().toUpperCase();
	}
	public static void main(String[] args) {
		System.out.println(ShaUtil.Encrypt("123123sgdsag", "SHA-256").toUpperCase());
	}
}
